package com.waiter.moximoxi.juph;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;


import com.waiter.moximoxi.MainActivity;
import com.waiter.moximoxi.ui.SettlementActivity;

import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.CustomMessage;
import cn.jpush.android.api.JPushMessage;
import cn.jpush.android.api.NotificationMessage;
import cn.yhong.aframework.utils.AtyUtils;

/**
 * 自定义接收器
 * <p>
 * 如果不定义这个 Receiver，则： 1) 默认用户会打开主界面 2) 接收不到自定义消息
 */
public class JPushReceiver extends cn.jpush.android.service.JPushMessageReceiver {

    @Override
    public void onConnected(Context context, boolean b) {
        super.onConnected(context, b);
    }

    @Override
    public void onTagOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onTagOperatorResult(context, jPushMessage);
    }

    @Override
    public void onCheckTagOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onCheckTagOperatorResult(context, jPushMessage);
    }

    @Override
    public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onAliasOperatorResult(context, jPushMessage);
    }

    @Override
    public void onMobileNumberOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onMobileNumberOperatorResult(context, jPushMessage);
    }


    /**
     * 收到自定义消息回调
     *
     * @param context
     * @param customMessage
     */
    @Override
    public void onMessage(Context context, CustomMessage customMessage) {
        super.onMessage(context, customMessage);
    }

    /**
     * 收到通知回调
     *
     * @param context
     * @param notificationMessage
     */
    @Override
    public void onNotifyMessageArrived(Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageArrived(context, notificationMessage);

        String extra = notificationMessage.notificationExtras;
        AtyUtils.i("通知消息", extra);

        if (!TextUtils.isEmpty(extra)) {
            try {
                JSONObject jsonObject = new JSONObject(extra);
                String msg_id = jsonObject.getString("msg_id");
                String data = jsonObject.getString("data");
                if (msg_id.equals("0")) {
                    //发送本地广播到主页面
                    LocalBroadcastManager.getInstance(context).sendBroadcast(
                            new Intent(MainActivity.ACTION_RECIVE_NEWRED).putExtra("extra", data));
                } else if (msg_id.equals("1")) {
                    //发送本地广播到订单页面
                    LocalBroadcastManager.getInstance(context).sendBroadcast(
                            new Intent(SettlementActivity.ACTION_RECIVE_NEWRED).putExtra("extra", data));
                }else if(msg_id.equals("2")){
                    //发送广播通知餐桌列表刷新
                    LocalBroadcastManager.getInstance(context).sendBroadcast(
                            new Intent(MainActivity.ACTION_RECIVE_NEWRED).putExtra("extra", jsonObject.getJSONArray("pcate_id").toString()));
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 点击通知回调
     *
     * @param context
     * @param notificationMessage
     */
    @Override
    public void onNotifyMessageOpened(Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageOpened(context, notificationMessage);
    }

    /**
     * 清除通知回调
     *
     * @param context
     * @param notificationMessage
     */
    @Override
    public void onNotifyMessageDismiss(Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageDismiss(context, notificationMessage);
    }

}
